<?php

/*	Vulpes - Web-based managegent for wireless internet providers.
	Copyright (C) 2007 LinFox Serviços em Informática Ltda.

	This file is part of Vulpes.

	Vulpes is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Vulpes is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.




/sbin/tc qdisc del dev eth1 root
/sbin/tc qdisc add dev eth1 root handle 1 cbq bandwidth 100Mbit avpkt 1000 cell 8
/sbin/tc class change dev eth1 root cbq weight 10Mbit allot 1514

/sbin/tc qdisc del dev eth3 root
/sbin/tc qdisc add dev eth3 root handle 1 cbq bandwidth 100Mbit avpkt 1000 cell 8
/sbin/tc class change dev eth3 root cbq weight 10Mbit allot 1514

/sbin/tc class add dev eth3 parent 1: classid 1:1020 cbq bandwidth 100Mbit rate 10Mbit weight 10Mbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
/sbin/tc qdisc add dev eth3 parent 1:1020 handle 1020 tbf rate 10Mbit buffer 10Kb/8 limit 15Kb mtu 1500
/sbin/tc filter add dev eth3 parent 1:0 protocol ip prio 100 u32 match ip dst 192.168.11.200 classid 1:1020

/sbin/tc class add dev eth1 parent 1: classid 1:1021 cbq bandwidth 100Mbit rate 10Mbit weight 10Mbit prio 5 allot 1514 cell 8 maxburst 20 avpkt 1000 bounded
/sbin/tc qdisc add dev eth1 parent 1:1021 handle 1021 tbf rate 10Mbit buffer 10Kb/8 limit 15Kb mtu 1500
/sbin/tc filter add dev eth1 parent 1:0 protocol ip prio 100 u32 match ip dst 200.129.73.161 classid 1:1021


*/

function query($query)
{
	global $conn;
	global $page;

	if (!$result = mysql_query($query, $conn))
	{
		$page->error('{lang_query_failed}', mysql_error(), $query);
		return false;
	}
	else
	{
		return $result;
	}

}

function get($var)
{
	if		(isset($_POST[$var]))	return mysql_real_escape_string($_POST[$var]);
	elseif	(isset($_GET[$var]))	return mysql_real_escape_string($_GET[$var]);
	else							return false;
}

function iptables_refresh()
{
	global $page;
	
	$sql_clients = 'SELECT * FROM `clients`';
	$result_clients = query($sql_clients);

	exec('sudo /sbin/iptables -F');
	while ($client = mysql_fetch_object($result_clients))
	{
		$iptables = 'sudo /sbin/iptables -A FORWARD -s ' .$client->ip. ' -m mac --mac-source ' .$client->mac. " -j {status}\n";
		$iptables .= 'sudo /sbin/iptables -A INPUT -s ' .$client->ip. ' -m mac --mac-source ' .$client->mac. " -j {status}\n";
		$iptables .= 'sudo /sbin/iptables -A OUTPUT -s ' .$client->ip. " -j {status}\n";
		if ($client->status == 'L')
			$iptables = str_replace('{status}', 'ACCEPT', $iptables);
		else
			$iptables = str_replace('{status}', 'DROP', $iptables);
				
		exec($iptables, $messages, $error);
		if ($error)
		{
			foreach ($messages as $message)
			{
				$page->error($error, $message, $iptables);
			}
		}
	}
	
}

function xml_iterate($object)
{
	$result = '';

	foreach ($object as $key => $value)
	{
		$result .= '<'.$key.'>' .$value. '</'.$key.'>';
	}
	
	return $result;
}

?>